#include "Space2DController.h"
#include <QDebug>

Space2DController::Space2DController(MainWindow* mainWindow,
                                     QObject* parent) :
    QObject(parent)
{
    space2D = new Space2D(mainWindow);
    mainWindow->setSpace2D(space2D);

    connect(mainWindow,SIGNAL(newVertexMessage(QList<QColor*>)),this,SLOT(newVertexHandler(QList<QColor*>)));
    connect(mainWindow,SIGNAL(newLineMessage(QList<QColor*>)),this,SLOT(newLineHandler(QList<QColor*>)));
    connect(mainWindow,SIGNAL(newTriangleMessage(bool,bool,QList<QColor*>)),this,SLOT(newTriangleHandler(bool,bool,QList<QColor*>)));
    connect(mainWindow,SIGNAL(newQuadrilateralMessage(bool,bool,QList<QColor*>)),this,SLOT(newQuadrilateralHandler(bool,bool,QList<QColor*>)));
    connect(mainWindow,SIGNAL(loadTextureMessage(QString)),this,SLOT(loadTextureHandler(QString)));
    connect(mainWindow,SIGNAL(cleanAllMessage()),this,SLOT(cleanAllHandler()));

    mainWindow->show();
}


void Space2DController::newVertexHandler(QList<QColor *> colors)
{
    if(space2D->numControlPoints() == 0)
    {
        space2D->setInitialNumControlPoints(1);
        space2D->setNumControlPoints(1);
        Drawing* dot = new Dot();
        dot->setColors(colors);
        space2D->setLastDraw(dot);
    }
}

void Space2DController::newLineHandler(QList<QColor *> colors)
{
    if(space2D->numControlPoints() == 0)
    {
        space2D->setInitialNumControlPoints(2);
        space2D->setNumControlPoints(2);
        Drawing* line = new Line();
        line->setColors(colors);
        space2D->setLastDraw(line);
    }
}

void Space2DController::newTriangleHandler(bool filled,bool wired,QList<QColor*> colors)
{
    if(space2D->numControlPoints() == 0)
    {
        space2D->setInitialNumControlPoints(3);
        space2D->setNumControlPoints(3);
        Drawing* triangle = new Triangle();
        triangle->setColors(colors);
        triangle->setFilled(filled);
        triangle->setWired(wired);
        space2D->setLastDraw(triangle);
    }
}

void Space2DController::newQuadrilateralHandler(bool filled,bool wired, QList<QColor*> colors)
{
    if(space2D->numControlPoints() == 0)
    {
        space2D->setInitialNumControlPoints(4);
        space2D->setNumControlPoints(4);
        Drawing* quadrilateral = new Quadrilateral();
        quadrilateral->setColors(colors);
        quadrilateral->setFilled(filled);
        quadrilateral->setWired(wired);
        space2D->setLastDraw(quadrilateral);
    }
}

void Space2DController::cleanAllHandler()
{
    space2D->cleanAll();
}

void Space2DController::loadTextureHandler(QString path)
{
    space2D->setLastSelectedTexture(new QImage(path));
}
